<template>
  <div class="login">
    <img src="https://web.zfqfu.com/static/img/logo.4c46f706.png" alt="" srcset="" class="logo" />
    <a-row>
      <a-col :span="18" class="content">
        <div class="card">
          <div class="title">欢迎使用 中福有票</div>
          <br />
          <a-form ref="formRef" name="custom-validation" :model="formState" v-bind="layout">
            <a-form-item name="userName" v-bind="validateInfos.userName">
              <a-input
                v-model:value="formState.userName"
                autocomplete="off"
                placeholder="请输入用户名"
                allowClear
              >
                <template #prefix>
                  <user-outlined :style="{ color: '#429bc4' }" />
                </template>
              </a-input>
            </a-form-item>
            <a-form-item name="phoneNumber" v-bind="validateInfos.phoneNumber">
              <a-input
                v-model:value="formState.phoneNumber"
                autocomplete="off"
                placeholder="请输入手机号"
                allowClear
              >
                <template #prefix>
                  <PhoneOutlined :style="{ color: '#429bc4' }" />
                </template>
              </a-input>
            </a-form-item>
            <a-form-item name="validateCode" v-bind="validateInfos.validateCode">
              <a-input
                v-model:value="formState.validateCode"
                autocomplete="off"
                allowClear
                placeholder="请输入验证码"
              >
                <template #prefix>
                  <SafetyOutlined :style="{ color: '#429bc4' }" />
                </template>
                <template #suffix>
                  <span
                    v-if="sendCodeDisabled"
                    style="cursor: pointer; font-size: 12px; color: #429bc4"
                    @click="sendCode"
                    >发送验证码</span
                  >
                  <a-statistic-countdown
                    v-if="!sendCodeDisabled"
                    :valueStyle="{ color: '#429bc4', fontSize: '12px' }"
                    :value="deadline"
                    format="ss"
                    @finish="countdown"
                  >
                    <template #suffix>
                      <span style="cursor: pointer; font-size: 12px; color: #429bc4">重新发送</span>
                    </template>
                  </a-statistic-countdown>
                </template>
              </a-input>
            </a-form-item>
            <a-form-item name="inviterId" v-bind="validateInfos.inviterId">
              <a-input
                v-model:value="formState.inviterId"
                autocomplete="off"
                allowClear
                placeholder="请输入邀请人ID"
              >
                <template #prefix>
                  <DingdingOutlined :style="{ color: '#429bc4' }" />
                </template>
              </a-input>
            </a-form-item>
            <a-form-item>
              <a-button
                type="primary"
                html-type="submit"
                style="width: 100%"
                @click.prevent="onSubmit"
                >登录</a-button
              >
            </a-form-item>
            <div style="color: #888888; font-size: 12px; text-align: center; margin-top: 15px">
              如需帮助请致电：400-8888-8888
            </div>
          </a-form>
        </div>

        <div class="footer">
          上海斯卜特数字科技有限公司丨沪ICP备19041325号丨沪公安备31011302005729
        </div>
      </a-col>
      <a-col :span="6" class="tips">
        <div>轻松注册，灵活出单，实时接单，自行提现。</div>
        <img src="../../assets/images/side-img.dc937ad2.png" alt="" srcset="" />
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import {
  UserOutlined,
  SafetyOutlined,
  DingdingOutlined,
  PhoneOutlined
} from '@ant-design/icons-vue'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import type { FormInstance } from 'ant-design-vue'
import { Form, message } from 'ant-design-vue'
import { biddingLogin } from '@/servers/user'
import { useUserStore } from '@/stores/useUser'
import { storeToRefs } from 'pinia'
import { sendValidateCode } from '@/servers/main'
const { changeUserInfo, changeAccessToken, changeUserId, changeExpiresIn } = useUserStore()
const Router = useRouter()
const useForm = Form.useForm
interface FormState {
  userName: string
  phoneNumber: number | string
  validateCode: number | string
  inviterId: number | string
}
let deadline: any = ref(null)
let sendCodeDisabled = ref(true)
// 验证码倒计时结束
function countdown() {
  sendCodeDisabled.value = true
}
// 发送验证码
function sendCode() {
  sendValidateCode({ phone: formState.phoneNumber }).then((response) => {
    console.log(response)
    if (response.code == 200) {
      message.success(response.data)
      deadline.value = Date.now() + 10 * 1000
      sendCodeDisabled.value = false
    }
  })
}
const formRef = ref<FormInstance>()
const formState = reactive<FormState>({
  userName: '',
  phoneNumber: '',
  validateCode: '',
  inviterId: ''
})
const rulesRef = reactive({
  userName: [
    {
      required: true,
      message: '用户名不能为空'
    }
  ],
  phoneNumber: [
    {
      required: true,
      message: '手机号不能为空！'
    }
    // {
    //   pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
    //   message: '手机号格式不正确！'
    // }
  ],
  validateCode: [
    {
      required: true,
      message: '验证码不能为空！'
    }
    // {
    //   min: 6,
    //   max: 6,
    //   message: '请输入6位数验证码',
    //   trigger: 'blur'
    // }
  ],
  inviterId: [
    {
      required: true,
      message: '邀请人ID不能为空'
    }
  ]
})
const layout = {
  wrapperCol: { span: 24 }
}
const { validate, validateInfos } = useForm(formState, rulesRef)
const onSubmit = () => {
  validate()
    .then(() => {
      console.log(formState)
      biddingLogin(formState).then((response) => {
        console.log(response)
        if (response.code == 200) {
          changeUserInfo(response.data.userInfo)
          changeAccessToken(response.data.access_token)
          changeUserId(response.data.userInfo.userId)
          changeExpiresIn(response.data.expires_in)
          Router.push('/')
        }
      })
    })
    .catch((err) => {
      console.log('error', err)
    })
}
</script>
<style scoped lang="less">
.login {
  position: relative;
  .logo {
    position: absolute;
    left: 37px;
    top: 31px;
    width: 106px;
  }
  .tips {
    background: #429bc4;
    height: 100vh;
    text-align: center;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    font-size: 24px;
    div {
      padding: 0 15px;
    }
  }
  .content {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    .card {
      box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
      // width: 370px;
      width: 390px;
      border-radius: 5px;
      padding: 60px 40px;
      .title {
        font-size: 24px;
        color: #000000;
        text-align: center;
        margin-bottom: 20px;
      }
    }
  }
  .footer {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 40px;
    line-height: 40px;
    text-align: center;
    color: #888888;
  }
}
// :deep .ant-statistic-content-value {
//   cursor: pointer;
//   font-size: 12px;
//   color: #429bc4;
// }
</style>
